<?php if (!defined('BASEPATH')) exit('No direct script access allowed'); 

class WeChatCallBack {
	
	public $_postObject;
	public $_fromUserName;
	public $_toUserName;
	public $_createTime;
	public $_msgType;
	public $_msgId;
	public $_time;

	public function __construct()
    {
		$CI =& get_instance();

		$CI->load->helper('common');
		$CI->load->library('TokenStub');
    }
	
	public function init($postObj) {
		// 获取参数
		$this->_postObject = $postObj;
		if ($this->_postObject == false) {
			return false;
		}
		$this->_fromUserName = ( string ) trim ( $this->_postObject->FromUserName );
		$this->_toUserName = ( string ) trim ( $this->_postObject->ToUserName );
		$this->_msgType = ( string ) trim ( $this->_postObject->MsgType );
		$this->_createTime = ( int ) trim ( $this->_postObject->CreateTime );
		$this->_msgId = ( int ) trim ( $this->_postObject->MsgId );
		$this->_time = time ();
		if(!($this->_fromUserName && $this->_toUserName && $this->_msgType)) {
			return false;
		}
		return true;
	}
	
	public function responseTextMsg($contentStr)
    {
		$time = time();
		$msgType = 'text';
		$textTpl = "<xml>
					<ToUserName><![CDATA[%s]]></ToUserName>
					<FromUserName><![CDATA[%s]]></FromUserName>
					<CreateTime>%s</CreateTime>
					<MsgType><![CDATA[%s]]></MsgType>
					<Content><![CDATA[%s]]></Content>
					<FuncFlag>0</FuncFlag>
					</xml>";
					
        $resultStr = sprintf($textTpl, $this->_fromUserName, $this->_toUserName, $time, $msgType, $contentStr);
        echo $resultStr;					
	}
	
	public function getUserInfo ($openid) {
		$token = TokenStub::getToken();
		$para = array(
			"access_token" => $token,
			"openid" => $openid
		);
		
		$url = WX_API_URL . "user/info";
		$ret = doCurlGetRequest($url, $para);
		$retData = json_decode($ret, true);
		return $retData;
	}
	
	public function getOpenidByPage ($code) {
		$url = 'https://api.weixin.qq.com/sns/oauth2/access_token';
		$para = array(
		  "appid" => WX_API_APPID,
		  "secret" => WX_API_APPSECRET,
		  "code" => $code,
		  "grant_type" => 'authorization_code'
		);
	  
	   $ret = doCurlGetRequest($url, $para);
	   $retData = json_decode($ret, true);
	   
	   return $retData['openid'];
	}
	
	public function test_db_conn () {
		$CI =& get_instance();
		$CI->load->model('User_model');
		$fansInfo = $this->getUserInfo('oevXEtyapxccghYG1DOeU9tKlwYI');
		$CI->User_model->add_user(
			array(
				'openid' 	=>	$fansInfo['openid'],
				'nickname' 	=>	$fansInfo['nickname'],
				'sex'		=>	$fansInfo['sex'],
				'province'	=>	$fansInfo['province'],
				'city'		=>	$fansInfo['city'],
				'country'	=>	$fansInfo['country'],
				'headimgurl'=>	$fansInfo['headimgurl'],
				'regtime'	=>	isset($fansInfo['subcribe_time']) ? $fansInfo['subcribe_time'] : time(),
				'regip'		=>	getIp()
			)
		);
		echo "test OK!";
	}
	
	public function process() {
		if($this->_msgType == 'event') {
			$event = (string) trim($this->_postObject->Event);
			if($event == 'subscribe') {
				$CI =& get_instance();
				$CI->load->model('User_model');
				$fansInfo = $this->getUserInfo($this->_fromUserName);
				$CI->User_model->add_user(
					array(
						'openid' 	=>	$fansInfo['openid'],
						'nickname' 	=>	$fansInfo['nickname'],
						'sex'		=>	$fansInfo['sex'],
						'province'	=>	$fansInfo['province'],
						'city'		=>	$fansInfo['city'],
						'country'	=>	$fansInfo['country'],
						'headimgurl'=>	$fansInfo['headimgurl'],
						'regtime'	=>	isset($fansInfo['subcribe_time']) ? $fansInfo['subcribe_time'] : time(),
						'regip'		=>	getIp()
					)
				);
				$contentStr = "你好，欢迎关注好男人陈赫！点击左下角首页，进入陈赫粉丝专属微世界".var_export($fansInfo, true);
				$this->responseTextMsg($contentStr);
			}
		}
	}
	
    
   		
}

?>
